package com.da.lon.wang.xlg.activity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.view.ViewPagerFixed;
import com.github.chrisbanes.photoview.PhotoView;
import com.umeng.analytics.MobclickAgent;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ImagePreviewActivity extends BaseActivity {

    private float x1 = 0;
    private float x2 = 0;
    private float y1 = 0;
    private float y2 = 0;

    private ViewPagerFixed vp_imagepreview;
    private List<PhotoView> imageViews = new ArrayList<>();
    private List<String> imageUrls = new ArrayList<String>();
    private int count = 0;
    private TextView tx_imagepreview_changtxt;
    private TextView tx_imagepreview_allcount;
    private int sonposition;

    private View view;
    private int positionNew;

    /**
     * 1从发布预览来，0其他地方
     */
    private int From;

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imagepreview);
        findViews();
        init();
        addListeners();
    }

    public void findViews() {
        vp_imagepreview = (ViewPagerFixed) findViewById(R.id.vp_imagepreview);

        vp_imagepreview.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    x1 = event.getX();
                    y1 = event.getY();
                }
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    x2 = event.getX();
                    y2 = event.getY();
                    if (x2 - x1 == 0 && y2 - y1 == 0) {
                        finish();
                    }
                }

                return false;
            }
        });

        tx_imagepreview_changtxt = (TextView) findViewById(R.id.tx_imagepreview_changtxt);
        tx_imagepreview_allcount = (TextView) findViewById(R.id.tx_imagepreview_allcount);
        // imageUrls.add("http://pic.nipic.com/2008-07-05/20087584237954_2.jpg");
        // imageUrls.add("http://pic4.nipic.com/20091107/3612993_200526041765_2.jpg");
        // imageUrls.add("http://www.fansimg.com/uploads2010/10/userid219491time20101013060759.jpg");
    }

    public void init() {

        /**
         * Bundle 接收图片字符串数组
         */
        try {
            From = getIntent().getIntExtra("FROM", 0);
            Bundle photeBundle = this.getIntent().getExtras();

            String[] photoStrings = photeBundle.getStringArray("PHOTOES");
            positionNew = photeBundle.getInt("position");

            for (int i = 0; photoStrings != null && i < photoStrings.length; i++) {
                imageUrls.add(photoStrings[i]);
            }
            sonposition = getIntent().getIntExtra("sonposition", 0);

            count = imageUrls.size();
            tx_imagepreview_allcount.setText(count + "");
            for (int i = 0; imageUrls != null && i < imageUrls.size(); i++) {
//				ImageView imageView = new ImageView(this);
                PhotoView imageView = new PhotoView(this);
                imageView.setTag(imageUrls.get(i));
                if (From == 1 && imageUrls.get(i) != null
                        && !imageUrls.get(i).isEmpty()) {
                    MyApplication.loadImageByPicasso(this, null, new File(imageUrls.get(i)), imageView, 0, 0, 1);
                } else if (imageUrls.get(i) != null
                        && !imageUrls.get(i).isEmpty()) {
                    MyApplication.loadImageByPicasso(this, imageUrls.get(i), null, imageView, 0, 0, 1);
                }
                imageView.setScaleType(ScaleType.FIT_CENTER);
                imageViews.add(imageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void addListeners() {
        vp_imagepreview.setAdapter(new ImagePreviewAdapter());
        vp_imagepreview.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                tx_imagepreview_changtxt.setText(arg0 + 1 + "");
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
        vp_imagepreview.setCurrentItem(positionNew);
    }

    class ImagePreviewAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return count;
        }

        @Override
        public Object instantiateItem(View container, int position) {
            ((ViewPager) container).addView(imageViews.get(position), 0);
            return imageViews.get(position);
        }

        @Override
        public void destroyItem(View arg0, int position, Object arg2) {
            ((ViewPager) arg0).removeView(imageViews.get(position));
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

    }

}
